<!DOCTYPE html>
<html>
<head>
  <title>Json2Arthas界面</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <style>
    .container {
      width: 1000px;
      margin: 50px auto;
    }

    .input-label {
      font-weight: bold;
      margin-bottom: 10px;
    }

    .input-field {
      width: 100%;
      height: 200px;
      padding: 5px;
      margin-bottom: 20px;
    }
    .output-field {
      width: 100%;
      height: 200px;
      padding: 5px;
    }

    .convert-btn {
      display: block;
      width: 100%;
      padding: 10px;
      margin-top: 20px;
      background-color: #4CAF50;
      color: white;
      text-align: center;
      cursor: pointer;
    }
  </style>
</head>
<body>
  <div class="container">
    <div class="input-label">请输入json格式的入参(class字段会被识别为全类名)</div>
    <textarea id="jsonInput" class="input-field"></textarea>
    <div class="input-label">ognl格式入参</div>
    <textarea id="arthasOutput" class="output-field"></textarea>
    <button id="convertBtn" class="convert-btn">xxxx为全类名，请自行替换</button>
  </div>

  <script>
    function convertJson2ArthasRender() {
      var jsonInput = document.getElementById('jsonInput').value;
      // 在这里编写将jsonInput转换为Arthas格式的逻辑
      let json2Ognl = new Json2Ognl();
      var arthasOutput = null;
      try {
        arthasOutput = json2Ognl.convertJsonStr2Arthas(jsonInput);
      } catch(error) {
        arthasOutput = "json解析异常"
      }
      
      document.getElementById('arthasOutput').value = arthasOutput;
    }

    var convertBtn = document.getElementById('convertBtn');
    convertBtn.addEventListener('click', convertJson2ArthasRender);
    var jsonInput = document.getElementById('jsonInput');
    jsonInput.addEventListener('input', function() {
      convertJson2ArthasRender();
    });

class ParseException extends Error {}

class Json2Ognl {
    constructor() {
        this.ClassKey = "class";
    }

    convertJsonStr2Arthas(jsonStr) {
        let resStream = [];
        return this.convertJson2Arthas(resStream, 0, null, JSON.parse(jsonStr)).join("");
    }

    parseSimpleArr(resStream, idx, jsonObject) {
        let isSimple = false;
        let curBasicStr = null;
        if (typeof jsonObject === "number") {
            curBasicStr = jsonObject.toString();
            isSimple = true;
        }
        if (typeof jsonObject === "string") {
            curBasicStr = `"${jsonObject}"`;
            isSimple = true;
        }
      
        if(isSimple) {
          if (idx > 0) {
            resStream.push(",");
        	}
          resStream.push(curBasicStr);
        }
        
        return isSimple;
    }

    writeListElement(resStream, idx, ognlName) {
        if (idx > 0) {
            resStream.push(",");
        }
        resStream.push(`#${ognlName}`);
    }

    convertJson2Arthas(resStream, depth, name, json) {
        let prefix = " ".repeat(depth);
        let instanceName = name != null ? name : `param${depth}`;
        if (Array.isArray(json)) {
            let name2JsonObject = {};
            resStream.push(`${prefix}#${instanceName} = {`);
            let childPrefixName = instanceName + "_";
            json.forEach((jsonEle, i) => {
                if (!this.parseSimpleArr(resStream, i, jsonEle)) {
                    let childName = childPrefixName + i;
                    this.writeListElement(resStream, i, childName);
                    name2JsonObject[childName] = jsonEle;
                }
            });
            resStream.push("}");
            resStream.push("\n")
            for (let [name, jsonObject] of Object.entries(name2JsonObject)) {
                this.convertJson2Arthas(resStream, depth + 1, name, jsonObject);
            }
            return resStream;
        }

        resStream.push(`${prefix}#${instanceName} = new ${this.getFullPathClass(json)}()`);
        let name2SubJsonValue = {};
        let eleIdx = 0;
        for (let [key, value] of Object.entries(json)) {
            if (this.ClassKey === key) {
                continue;
            }
            resStream.push(",");
            let setValue = `#${instanceName}.set${key.charAt(0).toUpperCase() + key.slice(1)}(`;
            if (typeof value === "object" && !Array.isArray(value)) {
                setValue += `#${key}`;
                name2SubJsonValue[key] = value;
            } else {
                if (typeof value === "string") {
                    setValue += `"${value}"`;
                } else if (typeof value === "number") {
                    setValue += value.toString();
                } else if (Array.isArray(value)) {
                    let listEleStream = ["{"];
                    value.forEach((ele, i) => {
                        if (!this.parseSimpleArr(listEleStream, i, ele)) {
                            let childName = `${key}_${i}`;
                            listEleStream.push(`#${childName}`);
                            name2SubJsonValue[childName] = ele;
                        }
                    });
                    listEleStream.push("}");
                    setValue += listEleStream.join("");
                } else {
                    throw new ParseException("无法识别的类型");
                }
            }
            setValue += ")";
            resStream.push(setValue);
            eleIdx += 1;
        }
        resStream.push("\n");
        for (let [subName, subJsonObject] of Object.entries(name2SubJsonValue)) {
            this.convertJson2Arthas(resStream, depth + 1, subName, subJsonObject);
        }
        return resStream;
    }

    getFullPathClass(jsonObject) {
        for (let [key, value] of Object.entries(jsonObject)) {
            if (this.ClassKey === key) {
                return value;
            }
        }
        return "xxxx";
    }
}

let jsonStr = `
{"businessType":4,"sceneType":1,"size":"100_150","class":"com.yyg.Test","subObject":{"subObjectF1":{"s_s_s":"ssss"}},"subObject2":{"subObject2F1":"s_s_v1"}}
`;
let json2Ognl = new Json2Ognl();
let res = json2Ognl.convertJsonStr2Arthas(jsonStr);
console.log(res);
  </script>
</body>
</html>